package com.jeeplus.modules.sys.service;

import com.google.common.collect.Sets;
import com.jeeplus.common.utils.DbUtils;
import com.jeeplus.common.utils.FileUtils;
import com.jeeplus.common.utils.io.FileUtil;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.modules.sys.entity.InterfaceList;
import com.jeeplus.modules.sys.entity.ServiceInfo;
import com.jeeplus.modules.test.grid.entity.TestCountry;
import com.jeeplus.modules.test.tree.dialog.entity.TestTree1;
import com.jeeplus.modules.tools.utils.MultiDBUtils;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.print.DocFlavor;
import javax.swing.tree.RowMapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @author Administrator
 * @title: DataHubService
 * @projectName jeeplus
 * @description: TODO
 * @date 2019/7/2416:43
 */
@Service
@Transactional(readOnly = true)
public class DataHubService {
    DbUtils dbUtils = new DbUtils();

    public Page<ServiceInfo> findPage(Page<ServiceInfo> page, ServiceInfo serviceInfo) {
        String sql = "select ServiceName,ServiceDescr,Ipaddress,Port,UserName,PassWd,ConnStrName,(select t.`name` from test_tree t where t.value=Platform) Platform,StoreNo from service_info";
        //FileUtils.writeToFile("d:\\jeeplus.log",page.toString(),false);
        page.setCount(MultiDBUtils.get("jeeplus").getJdbcTemplate().queryForObject(dbUtils.getCountString(sql), Integer.class));
        List<Map<String, Object>> list = MultiDBUtils.get("jeeplus").queryList(sql + " limit ?,? ", page.startRow(), page.getPageSize());
        page.setListData(list);
        return page;
    }

    public ServiceInfo get(String id) {
        ServiceInfo serviceInfo = new ServiceInfo();
        try {
            List<ServiceInfo> list = new ArrayList<>();
            list = MultiDBUtils.get("jeeplus").getJdbcTemplate().query("select ServiceName,ServiceDescr,Ipaddress,Port,UserName,PassWd,ConnStrName,(select t.`name` from test_tree t where t.value=Platform) Platform,StoreNo from service_info where ServiceName = ?  ", new Object[]{id}, new BeanPropertyRowMapper<ServiceInfo>(ServiceInfo.class));
            if (list.size() > 0) {
                serviceInfo = list.get(0);
            }
        } catch (Exception e) {
            System.out.println("操作异常");
            e.printStackTrace();
        }
        return serviceInfo;
    }
    public boolean save(ServiceInfo serviceInfo){
        try {
            //判断是否存在
            JdbcTemplate db=MultiDBUtils.get("jeeplus").getJdbcTemplate();
            if(db.queryForObject("select count(*) from service_info where ServiceName=?",new Object[]{serviceInfo.getServicename()},Integer.class)>0)
            {

            db.update("update service_info set ServiceDescr=?,Ipaddress=?,Port=?,UserName=?,PassWd=?,ConnStrName=?,Platform=?,StoreNo=? where ServiceName=?",
                    serviceInfo.getServicedescr(),serviceInfo.getIpaddress(),serviceInfo.getPort(),serviceInfo.getUsername(),
                    serviceInfo.getPasswd(),serviceInfo.getConnstrname(),serviceInfo.getPlatform(),serviceInfo.getStoreno(),serviceInfo.getServicename());
            }
            else {
                db.update("insert into service_info  (ServiceName,ServiceDescr,Ipaddress,Port,UserName,PassWd,ConnStrName,Platform,StoreNo) values(?,?,?,?,?,?,?,?,?)",
                        serviceInfo.getServicename(),serviceInfo.getServicedescr(),serviceInfo.getIpaddress(),serviceInfo.getPort(),serviceInfo.getUsername(),
                                serviceInfo.getPasswd(),serviceInfo.getConnstrname(),serviceInfo.getPlatform(),serviceInfo.getStoreno());
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    public boolean saveInterfaceList(InterfaceList interfaceList){
        try {
            JdbcTemplate db=MultiDBUtils.get("jeeplus").getJdbcTemplate();
            if(db.queryForObject("select count(*) from interface_list where InterfaceName=?",new Object[]{interfaceList.getinterfacename()},Integer.class)>0){
                db.update("update interface_list set SysName=?," +
                        "InterfaceType=?,Descr=?,Status=?,FileName=?,FilePath=?," +
                        "Platform=?,FileFormat=?,FileSuffix=?,ServiceList=? where InterfaceName=?",interfaceList.getSysname(),
                        interfaceList.getinterfacetype(),interfaceList.getDescr(),interfaceList.getStatus(),interfaceList.getFilename(),
                        interfaceList.getFilepath(),interfaceList.getPlatform(),interfaceList.getFileformat(),interfaceList.getFilesuffix(),
                        interfaceList.getServicelist(),interfaceList.getinterfacename());
            }
            else {
                db.update("insert into interface_list(SysName,InterfaceName,InterfaceType,Descr,Status,FileName,FilePath,Platform,FileFormat,FileSuffix,ServiceList) values(?,?,?,?,?,?,?,?,?,?,?)",
                        interfaceList.getSysname(),interfaceList.getinterfacename(),
                        interfaceList.getinterfacetype(),interfaceList.getDescr(),interfaceList.getStatus(),interfaceList.getFilename(),
                        interfaceList.getFilepath(),interfaceList.getPlatform(),interfaceList.getFileformat(),interfaceList.getFilesuffix(),
                        interfaceList.getServicelist());
            }
            return true;
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    public List<TestTree1> getTreeRow(){
        JdbcTemplate db=MultiDBUtils.get("jeeplus").getJdbcTemplate();
        return db.query("select * from test_tree where name=? and parent_id=0",new Object[]{"平台信息"},new BeanPropertyRowMapper<>(TestTree1.class));
    }
    public Page<InterfaceList> findPage(Page<InterfaceList> page, InterfaceList interfaceList) {
        try {
            String sql = "select SysName,InterfaceName,InterfaceType,Descr,Status,FileName,FilePath,Platform,FileFormat,FileSuffix,ServiceList from interface_list ";
            page.setCount(MultiDBUtils.get("jeeplus").getJdbcTemplate().queryForObject(dbUtils.getCountString(sql), Integer.class));
            List<Map<String, Object>> list = MultiDBUtils.get("jeeplus").queryList(sql + " limit ?,? ", page.startRow(), page.getPageSize());
            System.out.println(list);
            page.setListData(list);
        }catch (Exception e){
            System.out.println("异常信息");
            e.printStackTrace();
        }
        return page;
    }
    public InterfaceList getInterfaceList(String id) {
        InterfaceList interfaceList = new InterfaceList();
        try {
            List<InterfaceList> list = new ArrayList<>();
            list = MultiDBUtils.get("jeeplus").getJdbcTemplate().query("select SysName,InterfaceName,InterfaceType,Descr,Status,FileName,FilePath,Platform,FileFormat,FileSuffix,ServiceList from interface_list where SysName = ?  ", new Object[]{id}, new BeanPropertyRowMapper<InterfaceList>(InterfaceList.class));
            if (list.size() > 0) {
                interfaceList = list.get(0);
            }
        } catch (Exception e) {
            System.out.println("操作异常");
            e.printStackTrace();
        }
        return interfaceList;
    }
}
